package cn.luowb.checkchat.controller;


import cn.luowb.checkchat.common.context.UserContext;
import cn.luowb.checkchat.common.result.Result;
import cn.luowb.checkchat.common.web.Results;
import cn.luowb.checkchat.dto.req.LikeDTO;
import cn.luowb.checkchat.dto.resp.LikeCountsVO;
import cn.luowb.checkchat.dto.resp.MyLikesVO;
import cn.luowb.checkchat.dto.resp.ToggleLikeVO;
import cn.luowb.checkchat.service.LikeService;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/likes")
@Slf4j
public class LikeController {

    @Resource
    private LikeService likeService;

    @PostMapping("/toggle")
    public Result<ToggleLikeVO> toggleLike(@RequestBody LikeDTO likeDTO) {
        return likeService.togglelike(likeDTO.getType(),likeDTO.getObjId());
    }

    @GetMapping("/counts")
    public Result<LikeCountsVO> getLikeCounts(
            @Parameter(description = "对象类型（0=帖子，1=评论）", required = true)
            @RequestParam("type") int type,

            @Parameter(description = "目标ID列表；支持 ids=1,2,3 或 ids=1&ids=2", required = true)
            @RequestParam("ids") List<String> idsRaw
    ) {
        return likeService.getLikeCounts(type,idsRaw);
    }

    /**
     * 获取当前用户点赞列表
     */
    @GetMapping("/me")
    public Result<MyLikesVO> listMyLikes(
            @Parameter(description = "对象类型（0=帖子，1=评论）", required = true)
            @RequestParam("type") int type,
            @Parameter(description = "第几页（从 1 开始）")
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @Parameter(description = "每页条数（1~100）")
            @RequestParam(value = "pageSize", required = false, defaultValue = "20") int pageSize
    ) {
        // 获取当前用户 UID
        Long uid = UserContext.getUserId();
        if (uid == null || uid <= 0) {
            throw new RuntimeException("用户未登录");
        }
        if (type != 0 && type != 1) {
            throw new RuntimeException("参数不合法：type 仅允许 0/1");
        }
        if (page < 1) page = 1;
        if (pageSize < 1) pageSize = 1;
        if (pageSize > 100) pageSize = 100;

        MyLikesVO vo = likeService.listMyLikes(uid, type, page, pageSize);
        return Results.success(vo);
    }
}
